package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.ProductionImplementationAccept;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipSpareDao;
import com.efast.dmom.mes.dao.entity.OperationWorkorderDao;
import com.efast.dmom.mes.dao.entity.PlanDao;
import com.efast.dmom.mes.dao.entity.RouteStepDao;
import com.efast.dmom.mes.dao.entity.SubmittedWorkDao;
import com.efast.dmom.mes.dao.entity.SubmittedWorkRetentionDao;
import com.efast.dmom.mes.dao.entity.SubmittedWorkRetentionLogDao;
import com.efast.dmom.mes.entity.OperationWorkorderEntity;
import com.efast.dmom.mes.entity.PlanEntity;
import com.efast.dmom.mes.entity.ProductionPiecePartEntity;
import com.efast.dmom.mes.entity.RouteStepEntity;
import com.efast.dmom.mes.entity.SubmittedWorkEntity;
import com.efast.dmom.mes.entity.SubmittedWorkRetentionEntity;
import com.efast.dmom.mes.entity.SubmittedWorkRetentionLogEntity;
import com.efast.dmom.mes.service.EquipSpareService;
import com.efast.dmom.mes.service.ProductionPiecePartService;
import com.efast.dmom.mes.service.SubmittedWorkService;
import com.efast.dmom.mes.service.WmsStorageService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Transactional
@Service
public class SubmittedWorkServiceImpl implements SubmittedWorkService {

	@Autowired
	private SubmittedWorkDao submittedWorkDao;

	@Autowired
	private ProductionPiecePartService productionPiecePartService;

	@Autowired
	private WmsStorageService WmsStorageService;

//	@Autowired
//	private StartWorkService startWorkService;

	@Autowired
	private EquipSpareService equipSpareService;

	@Autowired
	private SubmittedWorkRetentionDao submittedWorkRetentionDao;

	@Autowired
	private PlanDao planDao;

	@Autowired
	private RouteStepDao routeStepDao;

	@Autowired
	private OperationWorkorderDao operationWorkorderDao;

	@Autowired
	private EquipSpareDao equipSpareDao;

	@Autowired
	private SubmittedWorkRetentionLogDao submittedWorkRetentionLogDao;

	@Override
	public ResultBean submittedWork(SubmittedWorkEntity submittedWorkEntity) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// 当前工单数据
		OperationWorkorderEntity operationWorkorderEntity2 = new OperationWorkorderEntity();
		operationWorkorderEntity2.setOrderNumber(submittedWorkEntity.getOrderNumber());
		operationWorkorderEntity2.setOperationId(submittedWorkEntity.getOperationId());
		OperationWorkorderEntity selectOne = operationWorkorderDao.selectOne(operationWorkorderEntity2);
		// 当为滞留报工时记录滞留总数量
		// 0:场内报,1:委外报,2滞留报
		if (submittedWorkEntity.getSubmittedWorkType() == 2) {
			if (submittedWorkEntity.getSubmittedWorkType() == 2) {
				SubmittedWorkRetentionEntity submittedWorkRetentionEntity2 = new SubmittedWorkRetentionEntity();
				submittedWorkRetentionEntity2.setPartId(submittedWorkEntity.getPartId());
				submittedWorkRetentionEntity2.setOperationId(submittedWorkEntity.getOperationId());
				List<SubmittedWorkRetentionEntity> select = submittedWorkRetentionDao
						.select(submittedWorkRetentionEntity2);
				if (select.size() > 0) {
					SubmittedWorkRetentionEntity submittedWorkRetentionEntity = new SubmittedWorkRetentionEntity();
					submittedWorkRetentionEntity
							.setRetentionQty(select.get(0).getRetentionQty() + submittedWorkEntity.getRetentionQty());
					submittedWorkRetentionEntity.setId(select.get(0).getId());
					submittedWorkRetentionEntity.setLastUpdateTime(TimeUtil.getLocalDateTime());
					submittedWorkRetentionEntity.setLastUpdateUser(SpringWebUtils.getRequestUser());
					submittedWorkRetentionDao.updateByPrimaryKeySelective(submittedWorkRetentionEntity);
				} else if (select.size() == 0) {
					SubmittedWorkRetentionEntity submittedWorkRetentionEntity = new SubmittedWorkRetentionEntity();
					submittedWorkRetentionEntity.setRetentionQty(submittedWorkEntity.getRetentionQty());
					submittedWorkRetentionEntity.setCreateTime(TimeUtil.getLocalDateTime());
					submittedWorkRetentionEntity.setCreateUser(SpringWebUtils.getRequestUser());
					submittedWorkRetentionEntity.setOperationId(submittedWorkEntity.getOperationId());
					submittedWorkRetentionEntity.setPartId(submittedWorkEntity.getPartId());
					submittedWorkRetentionDao.updateByPrimaryKeySelective(submittedWorkRetentionEntity);
				}
				// 记录滞留日志
				SubmittedWorkRetentionLogEntity submittedWorkRetentionLogEntity = new SubmittedWorkRetentionLogEntity();
				submittedWorkRetentionLogEntity.setCreateTime(TimeUtil.getLocalDateTime());
				submittedWorkRetentionLogEntity.setCreateUser(SpringWebUtils.getRequestUser());
				submittedWorkRetentionLogEntity.setOperationId(selectOne.getOperationId());
				submittedWorkRetentionLogEntity.setOperationType(selectOne.getOperationType());
				submittedWorkRetentionLogEntity.setOrderId(selectOne.getId());
				submittedWorkRetentionLogEntity.setPartId(selectOne.getPartId());
				submittedWorkRetentionLogEntity.setPlanId(selectOne.getPlanId());
				submittedWorkRetentionLogEntity.setQuatity(submittedWorkEntity.getRetentionQty());
				submittedWorkRetentionLogDao.insertSelective(submittedWorkRetentionLogEntity);
			}
			// 判断工单的数量是否报工完成
			List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber2 = submittedWorkDao
					.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity);
			long sum = 0L;
			if (selectByPartIdAndOperationIdAndOrderNumber2.size() > 0) {
				long orderQty = 0L;
				for (SubmittedWorkEntity submittedWorkEntity2 : selectByPartIdAndOperationIdAndOrderNumber2) {
					orderQty = orderQty + submittedWorkEntity2.getOutsourcingQty()
							+ submittedWorkEntity2.getSubmittedQty() + submittedWorkEntity2.getRetentionQty();
				}
				sum = orderQty + sum;
			}
			sum = sum + submittedWorkEntity.getSubmittedQty() + submittedWorkEntity.getOutsourcingQty()
					+ submittedWorkEntity.getRetentionQty();
			if (sum > selectOne.getQuatity()) {
				throw new RuntimeException("当前工单下可报工数量为" + selectOne.getQuatity() + ",实际报工数量为" + sum + ",请检查报工数量！");
//				resultBean.setErrorMessage("当前工单下可报工" + selectOne.getQuatity()+ "实际报工" + sum + "请检查报工数量！");
//				return resultBean;
			} else {
				OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
				operationWorkorderEntity.setId(selectOne.getId());
				// 更新完成数量
				operationWorkorderEntity
						.setFinishedQuatity(selectOne.getFinishedQuatity() + submittedWorkEntity.getSubmittedQty()
								+ submittedWorkEntity.getOutsourcingQty() + submittedWorkEntity.getRetentionQty());
				operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
				if (sum >= selectOne.getQuatity()) {
					updateOrderAndPlanStatus(selectOne);
				}

			}
			// 记录报工记录
			submittedWorkEntity.setCreationTime(TimeUtil.getLocalDateTime());
			submittedWorkEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
			submittedWorkEntity.setCreator(SpringWebUtils.getRequestUser());
			submittedWorkEntity.setSubmittedWorkDate(TimeUtil.getLocalDateTime());
			submittedWorkEntity.setRowStatus(0);
			submittedWorkDao.save(submittedWorkEntity);
			return resultBean;
		}
		// 通过计划id查询计划
		PlanEntity byId = planDao.getById(submittedWorkEntity.getPlanId());

		// 当前工单的报工总数量
		List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber = submittedWorkDao
				.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity);
		// 当计划数量和工单数量不同时
		if (selectOne.getQuatity() != byId.getQuantity()) {
			// 校验报工数量有没有超过滞留+当前工单的总数
//			SubmittedWorkRetentionEntity submittedWorkRetentionEntity2 = new SubmittedWorkRetentionEntity();
//			submittedWorkRetentionEntity2.setPartId(submittedWorkEntity.getPartId());
//			submittedWorkRetentionEntity2.setOperationId(submittedWorkEntity.getOperationId());
//			// 滞留数量
//			List<SubmittedWorkRetentionEntity> select = submittedWorkRetentionDao.select(submittedWorkRetentionEntity2);
			Long orderQty = 0L;
			if (selectByPartIdAndOperationIdAndOrderNumber.size() > 0) {
				for (SubmittedWorkEntity submittedWorkEntity2 : selectByPartIdAndOperationIdAndOrderNumber) {
					orderQty = orderQty + submittedWorkEntity2.getOutsourcingQty()
							+ submittedWorkEntity2.getSubmittedQty() + submittedWorkEntity2.getRetentionQty();
				}
			}
			// 计算出在物料下当前工序下工单的下所有报工总数量
			Long sum = submittedWorkEntity.getSubmittedQty() + submittedWorkEntity.getOutsourcingQty()
					+ submittedWorkEntity.getRetentionQty() + orderQty;
			// 当前报工总数量和工单数量对比
			if (sum > selectOne.getQuatity()) {
				throw new RuntimeException("当前工单下可报工数量为" + selectOne.getQuatity() + ",实际报工数量为" + sum + ",请检查报工数量！");
//				resultBean.setErrorMessage("当前工单下可报工" + selectOne.getQuatity() + "实际报工" + sum + "请检查报工数量！");
//				return resultBean;
			} else {
				OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
				operationWorkorderEntity.setId(selectOne.getId());
				// 更新完成数量
				operationWorkorderEntity
						.setFinishedQuatity(selectOne.getFinishedQuatity() + submittedWorkEntity.getSubmittedQty()
								+ submittedWorkEntity.getOutsourcingQty() + submittedWorkEntity.getRetentionQty());
				operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
				if (sum >= selectOne.getQuatity()) {
					updateOrderAndPlanStatus(selectOne);
				}
			}
		} else {
			// 当工单数量未改变计划数量等于工单数量时

			// 判断是否为首工序
			if (selectOne.getOperationType().equals("START")) {
				// 判断报工数量是否大于工单数量
				List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber2 = submittedWorkDao
						.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity);
				long sum = 0L;
				if (selectByPartIdAndOperationIdAndOrderNumber2.size() > 0) {
					long orderQty = 0L;
					for (SubmittedWorkEntity submittedWorkEntity2 : selectByPartIdAndOperationIdAndOrderNumber2) {
						orderQty = orderQty + submittedWorkEntity2.getOutsourcingQty()
								+ submittedWorkEntity2.getSubmittedQty() + submittedWorkEntity2.getRetentionQty();
					}
					sum = orderQty + sum;
				}
				sum = sum + submittedWorkEntity.getSubmittedQty() + submittedWorkEntity.getOutsourcingQty()
						+ submittedWorkEntity.getRetentionQty();
				if (sum > selectOne.getQuatity()) {
					throw new RuntimeException("报工总数量" + sum + ",不可大于工单数量" + selectOne.getQuatity() + "!");
//					resultBean.setErrorMessage("报工总数量" + sum + "不可大于工单数量" + selectOne.getQuatity());
//					return resultBean;
				} else {
					OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
					operationWorkorderEntity.setId(selectOne.getId());
					// 更新完成数量
					operationWorkorderEntity
							.setFinishedQuatity(selectOne.getFinishedQuatity() + submittedWorkEntity.getSubmittedQty()
									+ submittedWorkEntity.getOutsourcingQty() + submittedWorkEntity.getRetentionQty());
					operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
					if (sum >= selectOne.getQuatity()) {
						updateOrderAndPlanStatus(selectOne);
					}
				}
			} else {
				// 上一道工序数据
				OperationWorkorderEntity operationWorkorderEntity3 = new OperationWorkorderEntity();
				operationWorkorderEntity3.setPlanId(submittedWorkEntity.getPlanId());
				operationWorkorderEntity3.setSequence(selectOne.getSequence() - 1);
				OperationWorkorderEntity selectOne3 = operationWorkorderDao.selectOne(operationWorkorderEntity3);
				// 查询上一个工序当前工单的报工总数
//				SubmittedWorkEntity submittedWorkEntity2 = new SubmittedWorkEntity();
//				submittedWorkEntity2.setOperationId(selectOne3.getOperationId());
//				submittedWorkEntity2.setPartId(submittedWorkEntity.getPartId());
//				submittedWorkEntity2.setOrderNumber(selectOne3.getOrderNumber());
//				List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber1 = submittedWorkDao
//						.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity2);
				long previousOrderQty = selectOne3.getFinishedQuatity();
//				for (SubmittedWorkEntity submittedWorkEntity3 : selectByPartIdAndOperationIdAndOrderNumber1) {
//					previousOrderQty = previousOrderQty + submittedWorkEntity3.getSubmittedQty()
//							+ submittedWorkEntity3.getOutsourcingQty();
//				}
				// 当前工序的报工总数
				long currentOrderQty = 0L;
				for (SubmittedWorkEntity submittedWorkEntity3 : selectByPartIdAndOperationIdAndOrderNumber) {
					currentOrderQty = currentOrderQty + submittedWorkEntity3.getSubmittedQty()
							+ submittedWorkEntity3.getOutsourcingQty() + submittedWorkEntity3.getRetentionQty();
				}
				currentOrderQty = currentOrderQty + submittedWorkEntity.getSubmittedQty()
						+ submittedWorkEntity.getOutsourcingQty() + submittedWorkEntity.getRetentionQty();
				if (currentOrderQty > selectOne.getQuatity()) {
					throw new RuntimeException("报工总数量" + currentOrderQty + ",不可大于工单数量" + selectOne.getQuatity() + "!");
				}
				if (previousOrderQty < currentOrderQty) {
					throw new RuntimeException(
							"上一道工序当前工单报工总数为" + previousOrderQty + ",实际报工" + currentOrderQty + ",请检查报工数量！");
//					resultBean.setErrorMessage(
//							"上一道工序当前工单报工总数为" + previousOrderQty + "实际报工" + currentOrderQty + "请检查报工数量！");
//					return resultBean;
				} else {
					OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
					operationWorkorderEntity.setId(selectOne.getId());
					// 更新完成数量
					operationWorkorderEntity
							.setFinishedQuatity(selectOne.getFinishedQuatity() + submittedWorkEntity.getSubmittedQty()
									+ submittedWorkEntity.getOutsourcingQty() + submittedWorkEntity.getRetentionQty());
					operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
					if (selectOne.getQuatity() <= currentOrderQty) {
						// 更新工单状态
						updateOrderAndPlanStatus(selectOne);
					}

				}

			}

		}
		if (submittedWorkEntity.getSpareId() != 0) {
			int cutterQty = equipSpareDao.getCountByCutterCode(submittedWorkEntity.getSpareId());
			if (cutterQty == 0) {
				throw new RuntimeException("必须是已开工的备件才能报工！");
//			resultBean.setSuccess(false);
//			resultBean.setMessage("必须是已开工的备件才能报工！");
//			return resultBean;
			}
		}
		if (submittedWorkEntity.getBlankPiecePartId() != 0 && submittedWorkEntity.getWarehouseId() != 0) {
			ProductionImplementationAccept acceptBean = new ProductionImplementationAccept();
			acceptBean.setPartId(submittedWorkEntity.getPartId());
			acceptBean.setOperationId(submittedWorkEntity.getOperationId());
			acceptBean.setUser(submittedWorkEntity.getSubmittedWorkUser());
			acceptBean.setEquipId(submittedWorkEntity.getEquipId());
			acceptBean.setSpareId(submittedWorkEntity.getSpareId());
			ProductionPiecePartEntity piecePartEntity = productionPiecePartService.getProductionPiecePart(acceptBean);
			if (piecePartEntity == null) {
				ProductionPiecePartEntity productionPiecePartEntity = new ProductionPiecePartEntity();
				productionPiecePartEntity.setPiecePartId(submittedWorkEntity.getBlankPiecePartId());
				productionPiecePartEntity.setPiecePartQty(submittedWorkEntity.getBlankPiecePartQty());
				productionPiecePartEntity.setWarehouseId(submittedWorkEntity.getWarehouseId());
				productionPiecePartEntity.setPartId(submittedWorkEntity.getPartId());
				productionPiecePartEntity.setOperationId(submittedWorkEntity.getOperationId());
				productionPiecePartEntity.setOperationType(submittedWorkEntity.getOperationType());
				productionPiecePartEntity.setSubmittedWorkUser(submittedWorkEntity.getSubmittedWorkUser());
				productionPiecePartEntity.setSubmittedWorkDate(TimeUtil.getLocalDateTime());
				productionPiecePartEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
				productionPiecePartEntity.setCreationTime(TimeUtil.getLocalDateTime());
				productionPiecePartEntity.setCreator(SpringWebUtils.getRequestUser());
				productionPiecePartEntity.setIsFlag(0);
				productionPiecePartEntity.setRowStatus(0);
				productionPiecePartEntity.setEquipId(submittedWorkEntity.getEquipId());
				productionPiecePartEntity.setSpareId(submittedWorkEntity.getSpareId());
				productionPiecePartService.save(productionPiecePartEntity);

				// 毛坯件扣减
				ResultBean res = WmsStorageService.deductionPiecePartQty(submittedWorkEntity.getBlankPiecePartId(),
						submittedWorkEntity.getBlankPiecePartQty(), submittedWorkEntity.getWarehouseId(),
						submittedWorkEntity.getBlankPiecePartUnitPrice());
				if (!res.isSuccess()) {
					throw new RuntimeException(res.getMessage());
//					resultBean.setSuccess(false);
//					resultBean.setMessage(res.getMessage());
//					return resultBean;
				}
			} else {
				if (piecePartEntity.getPiecePartId() != submittedWorkEntity.getBlankPiecePartId()
						|| piecePartEntity.getPiecePartQty() != submittedWorkEntity.getBlankPiecePartQty()
						|| piecePartEntity.getEquipId() != submittedWorkEntity.getEquipId()
						|| piecePartEntity.getSpareId() != submittedWorkEntity.getSpareId()
						|| piecePartEntity.getWarehouseId() != submittedWorkEntity.getWarehouseId()) {
					ProductionPiecePartEntity productionPiecePartEntity = new ProductionPiecePartEntity();
					productionPiecePartEntity.setPiecePartId(submittedWorkEntity.getBlankPiecePartId());
					productionPiecePartEntity.setPiecePartQty(submittedWorkEntity.getBlankPiecePartQty());
					productionPiecePartEntity.setWarehouseId(submittedWorkEntity.getWarehouseId());
					productionPiecePartEntity.setPartId(submittedWorkEntity.getPartId());
					productionPiecePartEntity.setOperationId(submittedWorkEntity.getOperationId());
					productionPiecePartEntity.setOperationType(submittedWorkEntity.getOperationType());
					productionPiecePartEntity.setSubmittedWorkUser(submittedWorkEntity.getSubmittedWorkUser());
					productionPiecePartEntity.setSubmittedWorkDate(TimeUtil.getLocalDateTime());
					productionPiecePartEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
					productionPiecePartEntity.setCreationTime(TimeUtil.getLocalDateTime());
					productionPiecePartEntity.setCreator(SpringWebUtils.getRequestUser());
					productionPiecePartEntity.setIsFlag(0);
					productionPiecePartEntity.setRowStatus(0);
					productionPiecePartEntity.setEquipId(submittedWorkEntity.getEquipId());
					productionPiecePartEntity.setSpareId(submittedWorkEntity.getSpareId());
					productionPiecePartService.submittedWorkByPiecePart(productionPiecePartEntity);

					// 毛坯件扣减
					ResultBean res = WmsStorageService.deductionPiecePartQty(submittedWorkEntity.getBlankPiecePartId(),
							submittedWorkEntity.getBlankPiecePartQty(), submittedWorkEntity.getWarehouseId(),
							submittedWorkEntity.getBlankPiecePartUnitPrice());
					if (!res.isSuccess()) {
						throw new RuntimeException(res.getMessage());
//						resultBean.setSuccess(false);
//						resultBean.setMessage(res.getMessage());
//						return resultBean;
					}
				}
			}
		}
		if (submittedWorkEntity.getSpareId() != 0) {
			// 设备备件中刀具总次数增加
			equipSpareService.increaseCutterUsedTimes(submittedWorkEntity.getSubmittedQty(),
					submittedWorkEntity.getSpareId());
		}
		submittedWorkEntity.setCreationTime(TimeUtil.getLocalDateTime());
		submittedWorkEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		submittedWorkEntity.setCreator(SpringWebUtils.getRequestUser());
		submittedWorkEntity.setSubmittedWorkDate(TimeUtil.getLocalDateTime());
		submittedWorkEntity.setRowStatus(0);
		int savedata = submittedWorkDao.save(submittedWorkEntity);

		if (savedata > 0) {
			ProductionImplementationAccept acceptBean = new ProductionImplementationAccept();
			acceptBean.setUser(submittedWorkEntity.getSubmittedWorkUser());
			acceptBean.setPartId(submittedWorkEntity.getPartId());
			acceptBean.setOperationId(submittedWorkEntity.getOperationId());
			List<ProductionPiecePartEntity> productionPiecePartEntityList = productionPiecePartService
					.getProductionPiecePartList(acceptBean);
			resultBean.setObject(productionPiecePartEntityList);
			resultBean.setSuccess(true);
			resultBean.setMessage("报工成功！");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("报工失败！");
		}
		return resultBean;
	}

	/**
	 * 更新工单和计划状态
	 **/
	public void updateOrderAndPlanStatus(OperationWorkorderEntity operationWorkorderEntity) {
		OperationWorkorderEntity operationWorkorderEntity2 = new OperationWorkorderEntity();
		operationWorkorderEntity2.setId(operationWorkorderEntity.getId());
		operationWorkorderEntity2.setOrderStatus(1L);
		operationWorkorderEntity2.setLastUpdateTime(TimeUtil.getLocalDateTime());
		operationWorkorderEntity2.setLastUpdateUser(SpringWebUtils.getRequestUser());
		operationWorkorderEntity2.setCompleteTime(TimeUtil.getLocalDateTime());
		operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity2);
		if (operationWorkorderEntity.getOperationType().equals("END")) {
			PlanEntity planEntity = new PlanEntity();
			planEntity.setPlanStatus(40);
			planEntity.setId(operationWorkorderEntity.getPlanId());
			planEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
			planEntity.setLastModifier(SpringWebUtils.getRequestUser());
			planDao.updatePlanStatus(planEntity);
		}
	}

	// 当为委外报工时，专门处理数据
	public ResultBean submitOutSource(SubmittedWorkEntity submittedWorkEntity) {
		ResultBean resultBean = new ResultBean(true);
		List<Long> weiwaiOperationIds = submittedWorkEntity.getWeiwaiOperationIds();
		if (weiwaiOperationIds.size() < 1) {
			throw new RuntimeException("委外工序不可为空！");
		}
		// 当前的委外数量
		long outsourcingQty = submittedWorkEntity.getOutsourcingQty();
		// 当前所在的工序主键
		long operationId = submittedWorkEntity.getOperationId();

		// 当前计划数量
		PlanEntity byId = planDao.getById(submittedWorkEntity.getPlanId());
		// 遍历工步主键
		for (Long weiwaiOperationId : weiwaiOperationIds) {
			RouteStepEntity routeStepByKey = routeStepDao.getRouteStepByKey(weiwaiOperationId);
			// 将遍历的工序数组中的id一次放在里面方便新增
			submittedWorkEntity.setOperationId(routeStepByKey.getOperationKey());
			// 当前工单下工序对应数据
			OperationWorkorderEntity operationWorkorderEntity2 = new OperationWorkorderEntity();
			operationWorkorderEntity2.setPlanId(submittedWorkEntity.getPlanId());
			operationWorkorderEntity2.setOperationId(routeStepByKey.getOperationKey());
			OperationWorkorderEntity selectOne = operationWorkorderDao.selectOne(operationWorkorderEntity2);

			// 查询当前工序当前工单的报工总数
			SubmittedWorkEntity submittedWorkEntity3 = new SubmittedWorkEntity();
			submittedWorkEntity3.setOperationId(selectOne.getOperationId());
			submittedWorkEntity3.setPartId(submittedWorkEntity.getPartId());
			submittedWorkEntity3.setOrderNumber(selectOne.getOrderNumber());
			List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber2 = submittedWorkDao
					.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity3);
			// 遍历工序的报工总数
			long currentOrderQty = 0L;
			if (selectByPartIdAndOperationIdAndOrderNumber2.size() > 0) {
				for (SubmittedWorkEntity submittedWorkEntity31 : selectByPartIdAndOperationIdAndOrderNumber2) {
					currentOrderQty = currentOrderQty + submittedWorkEntity31.getSubmittedQty()
							+ submittedWorkEntity31.getOutsourcingQty() + submittedWorkEntity31.getRetentionQty();
				}
			}
			submittedWorkEntity.setOrderNumber(selectOne.getOrderNumber());
			// 当工单数量被修改
			if (selectOne.getQuatity() != byId.getQuantity()) {
				// 判断是否为首工序
				if (selectOne.getOperationType().equals("START")) {
					// 判断报工数量是否大于工单数量
					//当前工序
//					List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber21 = submittedWorkDao
//							.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity);
					long sum = 0L;
//					if (selectByPartIdAndOperationIdAndOrderNumber21.size() > 0) {
//						long orderQty = 0L;
//						for (SubmittedWorkEntity submittedWorkEntity2 : selectByPartIdAndOperationIdAndOrderNumber21) {
//							orderQty = orderQty + submittedWorkEntity2.getOutsourcingQty()
//									+ submittedWorkEntity2.getSubmittedQty() + submittedWorkEntity2.getRetentionQty();
//						}
//						sum = orderQty + sum;
//					}
//					sum = sum + submittedWorkEntity.getSubmittedQty() + submittedWorkEntity.getOutsourcingQty()
//							+ submittedWorkEntity.getRetentionQty();
					sum = currentOrderQty + outsourcingQty;
					if (sum > selectOne.getQuatity()) {
						throw new RuntimeException(routeStepByKey.getOperationName() + "工序当前工单下可报工数量为"
								+ selectOne.getQuatity() + ",实际报工" + sum + ",请检查报工数量！");
					} else {
						OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
						operationWorkorderEntity.setId(selectOne.getId());
						// 更新完成数量
						operationWorkorderEntity.setFinishedQuatity(selectOne.getFinishedQuatity()
								+ submittedWorkEntity.getSubmittedQty() + submittedWorkEntity.getOutsourcingQty()
								+ submittedWorkEntity.getRetentionQty());
						operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
						if (sum >= selectOne.getQuatity()) {
							updateOrderAndPlanStatus(selectOne);
						}
					}
				} else {
					// 上一道工序对应的工单数据
					OperationWorkorderEntity operationWorkorderEntity3 = new OperationWorkorderEntity();
					operationWorkorderEntity3.setPlanId(submittedWorkEntity.getPlanId());
					operationWorkorderEntity3.setSequence(selectOne.getSequence() - 1);
					OperationWorkorderEntity selectOne3 = operationWorkorderDao.selectOne(operationWorkorderEntity3);
					// 查询上一个工序当前工单的报工总数
					SubmittedWorkEntity submittedWorkEntity2 = new SubmittedWorkEntity();
					submittedWorkEntity2.setOperationId(selectOne3.getOperationId());
					submittedWorkEntity2.setPartId(submittedWorkEntity.getPartId());
					submittedWorkEntity2.setOrderNumber(selectOne3.getOrderNumber());
					List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber1 = submittedWorkDao
							.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity2);
					long previousOrderQty = 0L;
					for (SubmittedWorkEntity submittedWorkEntity31 : selectByPartIdAndOperationIdAndOrderNumber1) {
						previousOrderQty = previousOrderQty + submittedWorkEntity31.getSubmittedQty()
								+ submittedWorkEntity31.getOutsourcingQty();
					}
					currentOrderQty = currentOrderQty + outsourcingQty;
					if (currentOrderQty > selectOne.getQuatity()) {
						throw new RuntimeException(routeStepByKey.getOperationName() + "工序当前工单下可报工数量为"
								+ selectOne.getQuatity() + ",实际报工" + currentOrderQty + ",请检查报工数量！");
					}
					// 当前所在工序和操作工序为同一个工序是才进行和前一个工序判断是否满足条件
					if (previousOrderQty < currentOrderQty && submittedWorkEntity.getOperationId() == operationId) {
						throw new RuntimeException(
								"当前工序的上一道工序报工总数为" + previousOrderQty + ",实际报工" + currentOrderQty + ",请检查报工数量！");
					} else {
						OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
						operationWorkorderEntity.setId(selectOne.getId());
						// 更新完成数量
						operationWorkorderEntity.setFinishedQuatity(
								selectOne.getFinishedQuatity() + submittedWorkEntity.getOutsourcingQty());
						operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
						if (selectOne.getQuatity() <= currentOrderQty) {
							// 更新工单状态
							updateOrderAndPlanStatus(selectOne);
						}
					}
				}

			} else {
				// 当工单数量未被修改了
				// 校验报工数量有没有超过滞留+当前工单的总数
//				SubmittedWorkRetentionEntity submittedWorkRetentionEntity2 = new SubmittedWorkRetentionEntity();
//				submittedWorkRetentionEntity2.setPartId(submittedWorkEntity.getPartId());
//				submittedWorkRetentionEntity2.setOperationId(submittedWorkEntity.getOperationId());
				Long orderQty = 0L;
				if (selectByPartIdAndOperationIdAndOrderNumber2.size() > 0) {
					for (SubmittedWorkEntity submittedWorkEntity2 : selectByPartIdAndOperationIdAndOrderNumber2) {
						orderQty = orderQty + submittedWorkEntity2.getOutsourcingQty()
								+ submittedWorkEntity2.getSubmittedQty() + submittedWorkEntity2.getRetentionQty();
					}
				}
				// 计算出在物料下当前工序下工单的下所有报工总数量
				Long sum = submittedWorkEntity.getSubmittedQty() + submittedWorkEntity.getOutsourcingQty()
						+ submittedWorkEntity.getRetentionQty() + orderQty;
				// 当前报工总数量和工单数量对比操作工序为同一个工序是才进行和前一个工序判断是否满足条件
				if (sum > selectOne.getQuatity()) {
					throw new RuntimeException(routeStepByKey.getOperationName() + "工序当前工单下可报工数量为"
							+ selectOne.getQuatity() + ",实际报工" + sum + ",请检查报工数量！");
				}
				boolean flag = true;
				if (!selectOne.getOperationType().equals("START")) {
					// 上一道工序对应的工单数据
					OperationWorkorderEntity operationWorkorderEntity3 = new OperationWorkorderEntity();
					operationWorkorderEntity3.setPlanId(submittedWorkEntity.getPlanId());
					operationWorkorderEntity3.setSequence(selectOne.getSequence() - 1);
					OperationWorkorderEntity selectOne3 = operationWorkorderDao.selectOne(operationWorkorderEntity3);
					// 查询上一个工序当前工单的报工总数
					SubmittedWorkEntity submittedWorkEntity2 = new SubmittedWorkEntity();
					submittedWorkEntity2.setOperationId(selectOne3.getOperationId());
					submittedWorkEntity2.setPartId(submittedWorkEntity.getPartId());
					submittedWorkEntity2.setOrderNumber(selectOne3.getOrderNumber());
					List<SubmittedWorkEntity> selectByPartIdAndOperationIdAndOrderNumber1 = submittedWorkDao
							.selectByPartIdAndOperationIdAndOrderNumber(submittedWorkEntity2);
					long previousOrderQty = 0L;
					for (SubmittedWorkEntity submittedWorkEntity31 : selectByPartIdAndOperationIdAndOrderNumber1) {
						previousOrderQty = previousOrderQty + submittedWorkEntity31.getSubmittedQty()
								+ submittedWorkEntity31.getOutsourcingQty();
					}
					currentOrderQty = currentOrderQty + outsourcingQty;
					// 当前所在工序和操作工序为同一个工序是才进行和前一个工序判断是否满足条件
					if (previousOrderQty < currentOrderQty && submittedWorkEntity.getOperationId() == operationId) {
						throw new RuntimeException(
								"当前工序的上一道工序报工总数为" + previousOrderQty + ",实际报工" + currentOrderQty + ",请检查报工数量！");
					}
				}
				if (flag) {
					OperationWorkorderEntity operationWorkorderEntity = new OperationWorkorderEntity();
					operationWorkorderEntity.setId(selectOne.getId());
					// 更新完成数量
					operationWorkorderEntity.setFinishedQuatity(
							selectOne.getFinishedQuatity() + submittedWorkEntity.getOutsourcingQty());
					operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorderEntity);
					if (sum >= selectOne.getQuatity()) {
						updateOrderAndPlanStatus(selectOne);
					}
				}
			}

			// 记录报工记录
			submittedWorkEntity.setCreationTime(TimeUtil.getLocalDateTime());
			submittedWorkEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
			submittedWorkEntity.setCreator(SpringWebUtils.getRequestUser());
			submittedWorkEntity.setSubmittedWorkDate(TimeUtil.getLocalDateTime());
			submittedWorkEntity.setRowStatus(1);
			submittedWorkDao.save(submittedWorkEntity);
		}

		return resultBean;
	}

}
